package com.dolawing.phonecallapp.blacknumber;

import androidx.appcompat.app.AlertDialog;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dolawing.phonecallapp.R;
import com.dolawing.phonecallapp.blacknumber.db.BlackNumberDao;
import com.dolawing.phonecallapp.blacknumber.pojo.BlackNumber;

import java.util.List;

/**
 * 手机黑名单的实现
 *
 */
public class BlackNumberListActivity extends Activity implements View.OnClickListener{

    private ListView blacknumberListView;
    private TextView empty;
    private View view;
    private LayoutInflater mInflater;
    private EditText et_number_blacknumber_dialog;
    private Button bt_add_blacknumber , bt_modify_blacknumber;
    private Button bt_ok_blacknumber_dialog;
    private Button bt_cancel_blacknumber_dialog;
    private BlackNumberDao blackNumberDao;
    private AlertDialog dialog;
    private BlackNumberAdapter mAdapter;

    private int flag = 0;
    private final static int ADD = 1;
    private final static int UPDATE = 2;
    private String blacknumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.i("i", "  on create ");
        setContentView(R.layout.activity_black_number_list);

        blacknumberListView = (ListView) findViewById(R.id.blacknumberListView);
        empty = (TextView) findViewById(R.id.empty);
        bt_add_blacknumber = findViewById(R.id.addBlackNumberBtn);//添加黑名单
        bt_modify_blacknumber = findViewById(R.id.modifyBlackNumberBtn);//更新黑名单
        bt_add_blacknumber.setOnClickListener(this);
        bt_modify_blacknumber.setOnClickListener(this);

        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = mInflater.inflate(R.layout.add_blacknumber_dialog, null);
        et_number_blacknumber_dialog = (EditText) view.findViewById(R.id.et_number_blacknumber_dialog);
        bt_ok_blacknumber_dialog = (Button) view.findViewById(R.id.bt_ok_blacknumber_dialog);
        bt_cancel_blacknumber_dialog = (Button) view.findViewById(R.id.bt_cancel_blacknumber_dialog);
        bt_ok_blacknumber_dialog.setOnClickListener(this);
        bt_cancel_blacknumber_dialog.setOnClickListener(this);

        //当listview没有数据的显示内容
        blacknumberListView.setEmptyView(empty);

        blackNumberDao = new BlackNumberDao(this);
        List<BlackNumber> blacknumbers = blackNumberDao.findAll();
        mAdapter = new BlackNumberAdapter(this,R.layout.item_black_number_list, blacknumbers);
        blacknumberListView.setAdapter(mAdapter);

    }


    //按钮点击事件
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id = v.getId();
        switch (id) {
            case R.id.addBlackNumberBtn:
                ViewGroup parent = (ViewGroup) view.getParent();
                if(parent != null){
                    parent.removeAllViews();
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("添加黑名单");
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
                flag = ADD;
                break;
            case R.id.modifyBlackNumberBtn:
//                ViewGroup parent1 = (ViewGroup) view.getParent();
//                if(parent1 != null){
//                    parent1.removeAllViews();
//                }
//                et_number_blacknumber_dialog.setText(blacknumber);
//                AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
//                builder1.setTitle("更新黑名单");
//                builder1.setView(view);
//                dialog = builder1.create();
//                dialog.show();
//                flag = UPDATE;
                break;
            case R.id.bt_ok_blacknumber_dialog:
                String number = et_number_blacknumber_dialog.getText().toString();
                if("".equals(number)){
                    Toast.makeText(this, "黑名单号码不能为空", Toast.LENGTH_LONG).show();
                }else{
                    boolean isBlackNumber = blackNumberDao.isBlackNumber(number);
                    if(isBlackNumber){
                        Toast.makeText(this, "号码已经存在于黑名单中", Toast.LENGTH_LONG).show();
                    }else{
                        if(flag == ADD){
                            blackNumberDao.add(number);
                            Toast.makeText(this, "黑名单号码添加成功", Toast.LENGTH_LONG).show();
                        }else{
                            int _id = blackNumberDao.queryId(blacknumber);
                            blackNumberDao.update(_id, number);
                            Toast.makeText(this, "黑名单号码修改成功", Toast.LENGTH_LONG).show();
                        }

                        dialog.dismiss();

                        List<BlackNumber> blacknumbers = blackNumberDao.findAll();
                        mAdapter.setBlacknumbers(blacknumbers);
                        mAdapter.notifyDataSetChanged();//让listview自动刷新
                    }
                }

                break;
            case R.id.bt_cancel_blacknumber_dialog:
                dialog.dismiss();
                break;
            default:
                break;
        }
    }
}
